// pages/my/vip/vip_renew/index.js
const rechargeOrder=require("../../../controllers/ControllerVip").controller.rechargeOrder;
const payMent=require("../../../controllers/ControllerVip").controller.payMent;
const app=getApp();
const controller=require("../../../controllers/Controller").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalStype:'',
    price:null,
    desc:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.shopDiscount()

    this.setData({globalStype:app.globalData.globalStype})

    if(options.type){
      this.setData({
        type:options.type
      })
    }
    if(this.data.type=='cashOut'){
      this.setData({
        title:"提现",
        tips:"请输入提现金币数"
      })
    }else{
      this.setData({
        title:"充值续费",
        tips:"请输入充值金额（元）"
      })
    }

  },

  // 提现文案
  shopDiscount(){
    let value={}

    value.shopId=wx.getStorageSync('shopId')
    controller.shopDiscount(value).then(res=>{
      console.log(res);
      this.setData({
        desc:res.data.beanDesc
      })
    })
  },

  //金豆提现
  cashOutBean(){
    let value={}
    value.money=this.data.price
    value.shopId=wx.getStorageSync('shopId')
    if(!value.money){
      wx.showToast({
        icon:"none",
        title: '请输入提现金币数',
      })
      return
    }
    controller.accountWithdrawBean(value).then(res=>{
      wx.showToast({
        title: '操作成功',
        duration:1500
      })
      this.setData({
        price:""
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //获取充值金额
  inputNum(e){
    let price=e.detail.value;
    price=parseFloat(price);
    this.setData({
      price:price
    })
  },

  //进行充值
  getRechargeOrder(){
    let shopId=app.globalData.shopId;
    // if(this.data.price<100){
    //   wx.showToast({
    //     icon:"none",
    //     title: '最低充值金额为￥100',
    //   })
    // }else{
      rechargeOrder({price:this.data.price,shopId:shopId}).then(res=>{
        let orderNo=res.data.orderNo;
        let openId=app.globalData.openId
        payMent({shopId:shopId,orderNo:orderNo,openId:openId,frontUrl:"mp:///pages/Index/Index"}).then(res=>{
            if(res.code==1){
              // let url=res.data;
              // url=encodeURIComponent(url);
              // console.log(url);
              // wx.navigateTo({
              //   url: '/pages/weixinlink/weixinlink?url='+url,
              // })
              let data=JSON.parse(res.data);
              console.log(data)
              wx.requestPayment({
                nonceStr: data.nonceStr,
                package: data.package,
                paySign: data.paySign,
                timeStamp: data.timeStamp,
                signType: data.signType,
                success(res){
                  console.log(res)
                },
                fail(res){
                  console.log(res)
                }
              })
            }
        })
      })
    // }
  }
})